﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XNAProjectLibrary;
using XNAProject.GameElements;
using XNAProject.Core;
using XNAProject.PathFinder;

namespace XNAProject
{
    public sealed class PrepareState : BaseGameState, IPrepareState
    {
        private SpriteFont font;
        private SpriteFont bigFont;

        private Player player;
        private Enemy enemy;
        private Level level;
        private XNAProjectCamera camera;

        private PrepareStatePanel bottomPanel;

        private GameComponentCollection componentCollection;

        public PrepareState(Game game)
            : base(game)
        {
            game.Services.AddService(typeof(IPrepareState), this);

            componentCollection = new GameComponentCollection();
            Game.Services.AddService(typeof(GameComponentCollection), componentCollection);

            player = (Player)Game.Services.GetService(typeof(Player));

            enemy = new Enemy(Game);
            Game.Services.AddService(typeof(Enemy), enemy);

            level = new Level(Game, XNAProject.Enumerations.LevelSizeType.Big, 20000);
            componentCollection.Add(level);

            bottomPanel = new PrepareStatePanel(Game);
            componentCollection.Add(bottomPanel);

            camera = new XNAProjectCamera(OurGame, new Vector3(1600, 1000, 2000), new Vector4(level.size.Y * 66, level.size.Y * 340, level.size.X * 140, level.size.X * 30),
                new Vector2(1500, 600), 80, 120, 20, -30, 0);
            camera.UpdateInput = true;
            Game.Services.AddService(typeof(XNAProjectCamera), camera);
            componentCollection.Add(camera);

            foreach (GameComponent component in componentCollection)            
                Game.Components.Add(component);  
        }

        public void UnloadComponents()
        {
            foreach (GameComponent component in componentCollection)
            {
                Game.Components.Remove(component);
            }

            Game.Services.RemoveService(typeof(IPlayState));
            Game.Services.RemoveService(typeof(IPrepareState));
            Game.Services.RemoveService(typeof(XNAProjectCamera));
            Game.Services.RemoveService(typeof(IPathFinder));
            Game.Services.RemoveService(typeof(Chat));
            Game.Services.RemoveService(typeof(Enemy));
            player.ReSet();
            Game.Services.RemoveService(typeof(Level));
            Game.Services.RemoveService(typeof(ConsoleMenu));
            Game.Services.RemoveService(typeof(GameComponentCollection));
        }

        public override void Initialize()
        {
            //Game.GraphicsDevice.VertexDeclaration  = new VertexDeclaration(Game.GraphicsDevice, VertexPositionNormalTexture.VertexElements);
 
            //Game.GraphicsDevice.RenderState.CullMode = CullMode.None;    

            base.Initialize();
        }

        protected override void LoadContent()
        {
            font = Content.Load<SpriteFont>(@"Fonts\description");
            bigFont = Content.Load<SpriteFont>(@"Fonts\menu");  

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            if (Input.WasPressed(0, Buttons.Start, Keys.Enter))
            {
                OurGame.PlayState = new PlayState(OurGame);
                GameManager.ChangeState(OurGame.PlayState.Value);
                componentCollection.Remove(bottomPanel);
                Game.Components.Remove(bottomPanel);
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            OurGame.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);

            OurGame.spriteBatch.DrawString(font, "PrepareState  \n press Enter for PlayState", new Vector2(20, 20), Color.White);

            OurGame.spriteBatch.End();
            base.Draw(gameTime);
        }

       
    }
}
